<?xml version="1.0" ?>

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
						xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd"
                  version="1.0">

   <xsl:output method="text"/>

	<xsl:include href="../../common.xsl" />
	<xsl:include href="../../convert.xsl" />
   
   <xsl:template match="lzc:project">
		<xsl:text>-- #lazgen HEADER
-- Initial Data Script.
--
-- Populates table with initial values.

SET NOCOUNT ON;

-- #endlazgen HEADER
</xsl:text>
		<xsl:text>-- #lazgen BODY
</xsl:text>
		<xsl:for-each select="lzc:objects/lzc:object[ lzc:initial-data ]">
			<xsl:variable name="TABLE_NAME">
				<xsl:text>tbl</xsl:text>
				<xsl:value-of select="@name" />
			</xsl:variable>
			<xsl:text>
-- Initial Data for Table </xsl:text>
			<xsl:value-of select="$TABLE_NAME" />
			<xsl:text>
</xsl:text>
<xsl:if test="lzc:fields/lzc:field[ @type = $DATATYPE_IDENTITY ]">
<xsl:text>
SET IDENTITY_INSERT tbl</xsl:text><xsl:value-of select="@name" /><xsl:text> ON

</xsl:text>
</xsl:if>
			<xsl:for-each select="lzc:initial-data/lzc:data-row">
				<xsl:text>INSERT </xsl:text>
				<xsl:value-of select="$TABLE_NAME" />
				<xsl:text> ( </xsl:text>
				<xsl:for-each select="lzc:data-values/lzc:data-value">
					<xsl:value-of select="@field" />
					<xsl:if test="position() &lt; count( ../lzc:data-value )">
						<xsl:text>, </xsl:text>
					</xsl:if>
				</xsl:for-each>
				<xsl:text> ) VALUES ( </xsl:text>
				<xsl:for-each select="lzc:data-values/lzc:data-value">
					<xsl:variable name="VALUE_FIELD_NAME" select="@field" />
					<xsl:variable name="VALUE_FIELD" select="ancestor::lzc:object/lzc:fields/lzc:field[ @name = $VALUE_FIELD_NAME ]" />
					<xsl:call-template name="getSQLValue">
						<xsl:with-param name="pDataType" select="$VALUE_FIELD/@type" />
						<xsl:with-param name="pValue" select="@value" />
					</xsl:call-template>
					<xsl:if test="position() &lt; count( ../lzc:data-value )">
						<xsl:text>, </xsl:text>
					</xsl:if>
				</xsl:for-each>
				<xsl:text> );
</xsl:text>
			</xsl:for-each>
			<xsl:text>
</xsl:text>
<xsl:if test="lzc:fields/lzc:field[ @type = $DATATYPE_IDENTITY ]">
<xsl:text>SET IDENTITY_INSERT tbl</xsl:text><xsl:value-of select="@name" /><xsl:text> OFF

</xsl:text>
</xsl:if>
		</xsl:for-each>
		<xsl:text>
-- #endlazgen BODY

</xsl:text>
		<xsl:text>
-- #lazgen FOOTER

SET NOCOUNT OFF;

-- #endlazgen FOOTER
</xsl:text>
   </xsl:template>

</xsl:stylesheet>